/**
 * ANNCSR (2010-2011)
 */
package net.abbp.mtr;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.Calendar;
import java.util.List;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import net.abbp.core.util.DateUtils;
import net.abbp.dao.DaoFactory;
import net.abbp.dao.IDao;
import net.abbp.dao.condition.Cnds;
import net.abbp.dao.condition.Operator;
import net.abbp.mtr.config.ChannelManager;
import net.abbp.mtr.control.DateSelect;
import net.abbp.mtr.data.Record;
import net.abbp.mtr.event.DataArriveEvent;
import net.abbp.mtr.event.IDataArriveListener;
import net.abbp.mtr.i18n.I18N;

/**
 * 数据网格
 * 
 * @version 1.0 2010-12-18
 * @author lexloo(lexloo@gmail.com)
 * @since MTR 1.0
 */
public class DaqGrid extends JPanel {
    private JTable dataGrid;
    // 起始日期
    private Calendar startDate;
    // 临时日期，用于比较
    private Calendar compareDate = Calendar.getInstance();
    private DataModel dataModel;
    private IDao dao = DaoFactory.INSTANCE.newDao();

    public DaqGrid() {
        this.initComponents();

        this.setStartDate(Calendar.getInstance());
    }

    public IDataArriveListener getDataArriveListener() {
        return this.dataModel;
    }

    /**
     * 初始化
     */
    private void initComponents() {
        this.setLayout(new BorderLayout());
        this.dataModel = new DataModel();
        this.dataGrid = new JTable(this.dataModel);
        this.dataGrid.getTableHeader().setPreferredSize(new Dimension(10000, 24));
        this.dataGrid.setRowHeight(24);

        this.add(new JScrollPane(dataGrid));
        // this.add(new DatePane(), BorderLayout.SOUTH);
    }

    /**
	 * 
	 */
    private void initChannelData() {
        this.dataModel.clear();
        List<Record> recordList = dao.query(Record.class, Cnds.and("time", Operator.GreaterThanOrEqual, this
                .getStartDate()));

        for (Record r : recordList) {
            this.dataModel.addData(r.getChannel(), r);
        }
    }

    public void setStartDate(Calendar startDate) {
        this.startDate = DateUtils.getZeroCalendar(startDate.getTime());
        initChannelData();
    }

    public Calendar getStartDate() {
        return startDate;
    }

    private class DataModel extends DefaultTableModel implements IDataArriveListener {
        ChannelManager cm = ChannelManager.INSTANCE;

        public DataModel() {
            super(new String[] { "通道", "描述", "时间", "温度" }, 0);
        }

        public void clear() {
            this.dataVector.clear();
        }

        /**
         * 刷新
         * 
         * @param channel
         *            通道
         * @param data
         *            数据
         */
        @SuppressWarnings("unchecked")
        public void refresh(String channel, List<Record> data) {
            dataVector.clear();

            for (Record pd : data) {
                Vector row = new Vector();

                row.add(channel);
                row.add(cm.getChannelConfig(channel));
                row.add(pd.getTime());
                row.add(pd.getValue());

                dataVector.add(row);
            }

            this.fireTableDataChanged();
        }

        /**
         * 添加点
         * 
         * @param channel
         *            通道
         * @param pointData
         *            点数据
         */
        @SuppressWarnings("unchecked")
        public void addData(String channel, Record pointData) {
            Vector row = new Vector();

            row.add(channel);
            row.add(cm.getChannelConfig(channel));
            row.add(pointData.getTime());
            row.add(pointData.getValue());

            dataVector.add(0, row);

            this.fireTableDataChanged();
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            return columnType[columnIndex];
        }

        private Class<?>[] columnType = new Class[] { String.class, String.class, String.class, String.class };

        @Override
        public void dataArrive(DataArriveEvent evt) {
            // 判断数据是否当前日期，如果不是则不添加
            compareDate.setTime(evt.getData().getTime());
            if (compareDate.get(Calendar.DATE) > startDate.get(Calendar.DATE)) {
                DaqGrid.this.setStartDate(compareDate);
            }

            this.addData(evt.getChannel(), evt.getData());
        }
    }

    private class DatePane extends JPanel {
        /**
         * UID
         */
        private static final long serialVersionUID = -5004578352105519960L;

        private DatePane() {
            this.initComponents();
        }

        /**
         * 初始化
         */
        private void initComponents() {
            this.setLayout(new FlowLayout(FlowLayout.LEFT));

            this.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4),
                    BorderFactory.createTitledBorder("实现")));

            this.add(new JLabel(I18N.getLocText("daq.pane.startdate")));
            this.add(new DateSelect());
            this.add(new JLabel(I18N.getLocText("daq.pane.enddate")));
            this.add(new DateSelect());
            this.add(new JButton(I18N.getLocText("daq.pane.refresh")));
        }
    }
}
